﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Battle;

namespace ItemComponent
{
    /// <summary>
    /// 雷击
    /// </summary>
    public class LightningStrike:Cannon,IBulletHitHandler,IEnemyTargetGetter
    {
        protected Vector3 GetRandomPos()
        {
            return new Vector3(UnityEngine.Random.Range(-1f, 1f) * GameMgr.ScreenSize.x, GameMgr.ScreenSize.y + 1, 0);
        }

        protected override ST_BulletData GetBulletData(ICanHitObj target)
        {
            var tmp= base.GetBulletData(target);
            tmp.vBornPos = GetRandomPos();
            return tmp;
        }

        protected async override void FireAsync(ICanHitObj target)
        {
            if (target != null)
                await BulletFactory.CreateBulletAsync<Bullet_Electricity>(GetBulletData(target), BulletFactory.ElectricityPath, this, this);
        }

        public bool OnHit(IBullet bullet, ICanHitObj hitObj)
        {
            var enemys = EnemyMgr.Ins.GetEnemysInRange(bullet.Pos, (float)bullet.BulletData.rangeData.dRadius);

            for (int i = 0; i < enemys.Length; i++)
            {
                enemys[i].OnHit(new ST_HitActionData(bullet.BulletData.nDmg, bullet.BulletData.nRepelNum));
            }

            //爆炸特效
            EffMgr.CreateExplodeEffAsync(bullet.Pos, bullet.BulletData.rangeData, UnityEngine.Color.yellow);

            return true;
        }
        public ICanHitObj GetTarget(Vector3 pos, ICanHitObj[] otherTargets)
        {
            return EnemyMgr.Ins.GetTarget(transform.position, otherTargets);
        }

#if UNITY_EDITOR
        public new static STMyComponetData GetMyComponetData(ST_GunData gunData,Battle.ST_RangeData explodeData)
        {
            STMyComponetData componentData = Cannon.GetMyComponetData(gunData,explodeData);
            componentData.strType = typeof(LightningStrike).FullName;

            return componentData;
        }

   
#endif

    }
}
